package ExerciseOne;

import java.util.concurrent.locks.ReentrantLock;

public class ThreadSafe5 {
	public static void main(String[] args) {
		Window5 w = new Window5();
		Thread t1 = new Thread(w);
		Thread t2 = new Thread(w);
		t1.setName("线程一");
		t2.setName("线程二");
		t1.start();
		t2.start();
	}

}

class Window5 implements Runnable
{
	private int ticket =100;
	private ReentrantLock lock = new ReentrantLock();
	@Override
	public void run() {
		while(true)
		{
			try
			{	
				lock.lock();
				if(ticket>0)
				{
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName()+":"+ticket);
					ticket--;
				}
				else
				{
					break;
				}
			}finally
			{
				lock.unlock();
			}
		}
	}
	
}